package Demo.demo22;

import java.util.Scanner;

public class Game {

    Maps map = new Maps();
    Roles role = new Roles();
    /*初始化游戏*/
    public void initialize()
    {

        role.initializ();
    }
    /*游戏登陆界面*/
    public void beganInterface()
    {
        System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※");
        System.out.println("//                                                           //\t");
        System.out.println("//                                                          //\t");
        System.out.println("//                      骑士飞行棋                               //\t");
        System.out.println("//                                                        //\t");
        System.out.println("//                                                       //\t");
        System.out.println("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n\n\n");
        System.out.println("……………………………………………………………………两 人 对 战………………………………………………………………………………………………");
        //Roles role = new Roles();
        Scanner input =  new  Scanner(System.in);
        System.out.println("请选择角色：1.王保淞 2.向德才 3.王保淞2号 4.向德才2号");
        System.out.print("请选玩家1择角色：");
        int one = input.nextInt();
        boolean isRight = true;
        System.out.println();
        int two ;
        do
        {
            System.out.print("请选玩家2择角色：");
            two = input.nextInt();
            if(two==one)
            {
                System.out.println("玩家已存在，请从新选择！");
                isRight = false;
            }
            else
            {
                isRight = true;
            }
        }while(!isRight);
        switch(one)
        {
            case 1:
                role.roleNames[0] = "王保淞";
                break;
            case 2:
                role.roleNames[0] = "向德才";
                break;
            case 3:
                role.roleNames[0] = "王保淞2号";
                break;
            case 4:
                role.roleNames[0] = "王保淞2号";
                break;
        }
        switch( two)
        {
            case 1:
                role.roleNames[1] = "王保淞";
                break;
            case 2:
                role.roleNames[1] = "向德才";
                break;
            case 3:
                role.roleNames[1] = "王保淞2号";
                break;
            case 4:
                role.roleNames[1] = "王保淞2号";
                break;
        }


        //System.out.println(role.roleNames[0]+role.roleNames[1]);
    }
    /*开始游戏*/
    public void playGame()
    {
        System.out.println("*******************************************************************************\n");
        System.out.println("　　　　　　　　　　　　　　　　　　　　　GAME　START　　　　　　　　　　　　　　　　　　　　　\n");
        System.out.println("*******************************************************************************\n\n");
        System.out.println("☆"+role.roleNames[0]+"的士兵：A");
        System.out.println("☆"+role.roleNames[1]+"的士兵：B");
        System.out.println("\n\n");
        System.out.println("图列：∷.普通 ¤.地雷 〓.时空隧道 ■.暂停 ★.幸运轮盘\n\n ");
        map.showMap(0, 0);
    }
    /*开始游戏*/
    public void playing()
    {
        boolean isTurnOne =true;//记录谁抛色子


        do
        {
            if(role.indexs[0]<99&&role.indexs[1]<99)
            {

                do
                {

                    if(isTurnOne&&role.onOrPauses[0])
                    {
                        setDice(0);
                        isTurnOne =false;
                        role.onOrPauses[1]= true;

                    }

                    else if(!isTurnOne&&role.onOrPauses[1])
                    {
                        setDice(1);
                        isTurnOne =true;
                        role.onOrPauses[1]= true;
                    }
                    else if(isTurnOne&&!role.onOrPauses[0])
                    {
                        setDice(1);
                        isTurnOne =true;
                        role.onOrPauses[0]= true;
                    }
                    else if(isTurnOne&&!role.onOrPauses[1])
                    {
                        setDice(0);
                        isTurnOne =false;
                        role.onOrPauses[1]= true;
                    }
                }while(isTurnOne);

            }

            else if(role.indexs[0]>=99&&role.indexs[1]<=99)
            {
                System.out.println(role.roleNames[0]+"到达终点，获得胜利，游戏结束！");
                break;
            }
            else if(role.indexs[1]>=99&&role.indexs[0]<=99)
            {
                System.out.println(role.roleNames[1]+"到达终点，获得胜利，游戏结束！");
                break;
            }
            else
            {
                System.out.print(role.indexs[0]+" "+role.roleNames[1]+"你们都到达终点，游戏结束，）");
                break;
            }


        }while(true);

    }
    /*投色字后判断位置*/
    public void setDice(int i)
    {

        System.out.print(role.roleNames[i]+"请入任意字母后回车启动：");
        Scanner input = new Scanner(System.in);
        String sc = input.next();
        System.out.println("\n\n");
        if(sc!=null)
        {
            role.indexs[i]+=(int)(Math.random()*10);
            role.counts[i]++;
            switch(role.indexs[i])
            {
                case 9:
                case 27:
                case 60:
                case 93:
                    System.out.println("悲剧啊，你要暂停一次");
                    role.onOrPauses[i]=false;
                    break;
                case 6:
                case 23:
                case 40:
                case 55:
                case 69:
                case 83:

                    boolean isRight = true;
                    do
                    {
                        System.out.print("你运气真好，给你两种选择：1.和对方骑兵交换位置 2.轰炸对方骑兵，对方骑兵被炸退6步:");
                        String num = input.next();
                        if(num.equals("1"))
                        {
                            int tep = role.indexs[i];
                            role.indexs[i] = role.indexs[1-i];
                            role.indexs[1-i] = tep;
                            isRight = true;
                        }
                        else if(num.equals("2"))
                        {
                            if(role.indexs[1-i]>=6)
                            { role.indexs[1-i] = role.indexs[1-i]-6;

                            }
                            else
                            {
                                role.indexs[1-i]=0;
                            }
                            isRight = true;
                        }
                        else
                        {
                            System.out.println("输入错误请重新输入！");
                        }
                    }while(!isRight);
                    break;
                case 20:
                case 25:
                case 45:
                case 63:
                case 72:
                case 88:
                case 90:
                    role.counts[i]+=10;
                    System.out.println("运气真好，碰到时光隧道了！");
                    break;
                case 5:
                case 13:
                case 17:
                case 33:
                case 50:
                case 64:
                case 80:
                case 94:
                    if(  role.indexs[i]>=6)
                    {role.indexs[i]-=6;}
                    else
                    {
                        role.indexs[i]=0;
                    }
                    System.out.println("运气真差，踩到地雷了！");
                    break;
                default:
                    if(role.indexs[i]==role.indexs[1-i])
                    {
                        System.out.println("哈哈，踩到了！");
                        role.indexs[i] = role.indexs[1-i];
                        role.indexs[1-i] = 0;
                    }
                    break;
            }
            System.out.println("--------------------------------------------------------");
            System.out.println("骰子数："+role.counts[i]+"\n\n");
            System.out.println(role.roleNames[i]+"的当前位置："+role.indexs[i]);
            System.out.println("对方的当前位置："+role.indexs[1-i]);
            map.showMap(role.indexs[i],role.indexs[1-i] );
        }
    }
}

//地图类/
class Maps {

    //1.∷2.■3.☻4.〓5.☠
    int[] imgLables = new int[100];// 地图图像标号

    int[] luckyTurn = { 6, 23, 40, 55, 69, 83 }; // 幸运轮盘

    int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; // 地雷位置

    int[] pause = { 9, 27, 60, 93 }; // 暂停

    int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; // 时空隧道

    /* 创建地图 */
    public void createMap()
    {
        /* 读入幸运轮盘 */
        for (int i = 0; i < luckyTurn.length; i++)
        {
            imgLables[luckyTurn[i]] = 3;
        }
        /* 读入地雷位置 */
        for (int i = 0; i < landMine.length; i++)
        {
            imgLables[landMine[i]] = 5;
        }
        /* 读入暂停 */
        for (int i = 0; i < pause.length; i++)
        {
            imgLables[pause[i]] = 2;
        }
        /* 读入时光隧道 */
        for (int i = 0; i < timeTunnel.length; i++)
        {
            imgLables[timeTunnel[i]] = 4;
        }
        /* 普通 */
        for (int i = 0; i < imgLables.length; i++)
        {
            if (imgLables[i] == 0)
            {
                imgLables[i] = 1;
            }
        }
    }
    /**
     * 显示地图关卡对应的图形
     *
     * @param i
     *            地图当前位置的关卡代号
     * @param index
     *            当前地图位置编号
     * @param playerPos1
     *            玩家1 的当前位置
     * @param playerPos2
     *            玩家2 的当前位置
     * @return 地图当前位置的图形
     * 1.代表玩家在地图上，0代表不在
     */
    public String getGraph(int i, int index, int playerPos1, int playerPos2)
    {
        String graph = "";
        if ( playerPos1==index&&playerPos2==index)
        { //两个玩家都位于地图当前位置index
            graph = "@@";
        }
        else if (playerPos1==index)
        { //玩家 1 位于地图当前位置
            graph = "Ａ";
        }
        else if (playerPos2==index)
        { //玩家 2 位于地图当前位置
            graph = "Ｂ";
        }
        else
        {
            switch (i)
            {
                //关卡代号为1时：graph = "¤";
                //关卡代号为2时：graph = "★";
                //关卡代号为3时：graph = "■";
                //关卡代号为4时：graph = "〓";
                //其他：graph = "∷";
                case 1:
                    graph = "∷";
                    break;
                case 2:
                    graph = "■";
                    break;
                case 3:
                    graph = "¤";
                    break;
                case 4:
                    graph = "〓";
                    break;
                case 5:
                    graph = "★";
                    break;

            }
        }
        return graph;
    }
    public void showMap(int  playerPos1, int playerPos2 )
    {
        createMap();
        showLine(0,30,playerPos1, playerPos2);
        showRightColumm(31, 34,playerPos1 , playerPos2);
        showMiddleLine(35, 65,playerPos1 , playerPos2 );
        showLeftColumm(66,68,playerPos1, playerPos2);
        showLine(69,99,0,0);
    }
    public void showLine(int start,int end,int  playerPos1, int playerPos2)
    {
        for(int k=start;k<=end;k++)
        {
            System.out.print(getGraph(imgLables[k],k, playerPos1,playerPos2));
        }
        System.out.println();
    }
    public void showMiddleLine(int start,int end,int  playerPos1, int playerPos2)
    {
        for(int k=end;k>=start;k--)
        {
            System.out.print(getGraph(imgLables[k],k, playerPos1,playerPos2));
        }
        System.out.println();
    }
    public void showRightColumm(int start,int end,int  playerPos1, int playerPos2)
    {
        for(int c = start;c<=end;c++)
        {
            for(int i = 0;i<=29;i++)
            {
                System.out.print("　");
            }
            System.out.print(getGraph(imgLables[c],c, playerPos1,playerPos2));
            System.out.println();
        }
    }
    public void showLeftColumm(int start,int end,int  playerPos1, int playerPos2)
    {
        for(int c = start;c<=end;c++)
        {
            System.out.print(getGraph(imgLables[c],c, playerPos1,playerPos2));
            for(int i = 0;i<=29;i++)
            {
                System.out.print("　");
            }
            System.out.println();
        }
    }
}

class Roles {

    int[] indexs  = new int[2];//记录角色在地图上的位置
    boolean[] onOrPauses = new boolean[2];//记录角色是可以投骰子还是暂停
    String[] roleNames = new String[2];//记录角色名字
    int [] counts = new int[2];//记录角色投的次数
    /*初始化角色*/
    public void initializ()
    {
        indexs[0]= 0;
        indexs[1]= 0;
        onOrPauses [0] = true;
        onOrPauses [1] = true;
        roleNames[0] = "";
        roleNames[1] = "";
        counts [0] = 0;
        counts [1] = 0;

    }
    /*储存角色信息*/
    public  void storeRoleInf(int[] rindexs,boolean[] status,String[] rnames)
    {
        indexs =  rindexs;
        onOrPauses = status;
        roleNames = rnames;
    }
}
//测试类/
class Test {
    public static void main(String[] wzq)
    {

        Game b = new Game();
        b.initialize();
        b.beganInterface();
        b.playGame();
        b.playing();

    }
}
